package com.itheima.app.manager;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.*;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.dbvo.PageBeanVo;
import com.itheima.vo.dbvo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class SettingManager {
    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 查询通用设置
    public ResponseEntity findSettingVo() {
    //从线程中获取对象
        Long userId = UserHolder.get().getId();
        // 2.查询陌生人问题
        Question question = questionService.findByUserId(userId);
        //3.查询推送通知
        Notification notification = notificationService.findByUserId(userId);
        //4.封装settingVo
        SettingVo settingVo = new SettingVo();
        settingVo.setId(userId); //用户id
        settingVo.setPhone(UserHolder.get().getPhone());//用户手机号
        //用户陌生人问题
        if (question!=null){
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        //用户推送通知
        if (notification!=null){
            settingVo.setLikeNotification(notification.getLikeNotification());
            settingVo.setGonggaoNotification(notification.getGonggaoNotification());
            settingVo.setPinglunNotification(notification.getPinglunNotification());
        }
        //5.返回vo
        return ResponseEntity.ok(settingVo);
    }

    //设置陌生人问题
    public void setQuestion(String content) {
        //1.获取线程中用户的id
        //2.查询此用户的陌生人的问题
        //3.判断是否为空
        Long userId = UserHolder.get().getId();
        Question question = questionService.findByUserId(userId);
        if (question!=null){
               question.setStrangerQuestion(content);
               questionService.update(question);//修改问题
        }else {
            question = new Question();     //添加问题
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        }


    }

    //设置推送通知
    public void setNotifications(Notification parm) {
        //获取线程中当前对象的id
        //根据用户的id查询推送通知
        //判断
        Long userId = UserHolder.get().getId();
        Notification notification= notificationService.findByUserId(userId);

        if (notification!=null){
            notification.setLikeNotification(parm.getLikeNotification());
            notification.setGonggaoNotification(parm.getGonggaoNotification());
            notification.setPinglunNotification(parm.getPinglunNotification());
            notificationService.update(notification);
        }else {
            notification.setId(userId);
            notificationService.save(notification);
        }

    }

    //黑名单用户查询
    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        //获取当前线程中得对象id
        Long userId = UserHolder.get().getId();
        //调用分页对象
        PageBeanVo pageBeanVo = blackListService.findByPage(userId, pageNum, pageSize);
        //封装用户
        List<BlackList> blackListList = (List<BlackList>) pageBeanVo.getItems();
        //封装实体
        ArrayList<UserInfo> userInfoList = new ArrayList<>();
        //遍历
        if (blackListList!=null && blackListList.size()>0){
            for (BlackList blackList : blackListList) {
                //获取黑名单的userid
                Long blackUserId = blackList.getBlackUserId();
                //查询用户详情
                UserInfo userInfo = userInfoService.findUserInfoById(blackUserId);
                // 添加到集合中
                userInfoList.add(userInfo);
            }
        }
        //将userInfo设置到pageBeanvo中
        pageBeanVo.setItems(userInfoList);
        //返回vo
        return ResponseEntity.ok(pageBeanVo);

    }

    public void deleteByCondition(Long blackUserId) {
        //获取toker中userID
        Long userId = UserHolder.get().getId();
        // 2.调用rpc删除
        blackListService.deleteByCondition(userId,blackUserId);

    }

    public void sendSms() {
        //获取token中得userid
        User user = UserHolder.get();
        //生成6位随机数
        String smsCode = RandomUtil.randomNumbers(6);
        smsCode="123456";
        //调用阿里云发送
        //  smsTemplate.sendSms(user.getPhone(),smsCode);
        //将验证码存储到redis中
        stringRedisTemplate.opsForValue().set(ConstantUtil.SMS_CODE+user.getPhone(),smsCode, Duration.ofMinutes(5));


    }

    public ResponseEntity checkNum(String verificationCode) {
        //声明一个返回结果
        HashMap<String, Object> resultmap = new HashMap<>();
        //获取token中得user
        User user = UserHolder.get();
        String RedisNum = stringRedisTemplate.opsForValue().get(ConstantUtil.SMS_CODE + user.getPhone());
        if (!StrUtil.equals(verificationCode,RedisNum)){
            resultmap.put("verification",false);
            return ResponseEntity.ok(resultmap);

        }
        resultmap.put("verification",true);
        return ResponseEntity.ok(resultmap);
    }

    public void savePhone(String phone) {
        //获取token中得userid
        User user = UserHolder.get();
           user.setPhone(phone);
           user.setId(user.getId());
         userService.savePhone(user);
    }
}
